
#ifndef _MEMBER_BSCRIPT_H_
#define _MEMBER_BSCRIPT_H_

#include "IVariantStack.h"
#include "member_BSource.h"

#ifdef linux
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#endif

#ifdef _WIN32
#include <Windows.h>
#endif

#define script_member(script)	((member_BScript*)((BScript*)script)->_ptr)

namespace BWE
{
	struct CallInfo : public BThing
	{
		CallInfo()
		{
			pos = -1;
			top = -1;
			self = 0;
		}
		int			pos;
		int			top;
		BString		type;
		BClass*		self;
	};

	class member_BScript
	{
	public:
		static BMap<BString, BClassHolder>	basicClassMap;
		static BMap<BString, BClassHolder>	basicTemplateMap;

		struct Presets
		{
			BSet<BString>					includes;
			BMap<BString, BClassHolder>		classMap;
			BMap<BString, FunctionHolder>	functionMap;
		};

	public:
		member_BScript(BScript* script);
		~member_BScript();

		bool include(const BString& libname);

		BString& errorMessage(BExp* exp = 0);
		void sourceMessage(const BValue& value);

		void combine();
		void flatten();
		void flatten(BExp* exp);
		void link();
		void debug(BExp* exp);
		void debugResume();

		int perform(BExp* exp);
		BVariant* variant(const BString& name);
		BVariant* findVariant(const BString& name);
		Function* findFunction(const BString& name);

	public:
		BScript*				boss;
		Presets					presets;
		BArray<BSourceHolder>	sources;
		bool					debugging;
		BSet<BString>			breakpointSet;

		bool					error;
		BValue					message;
		bool					breaked;
		bool					running;
		BString					command;
		int						global;
		int						current;
		IVariantStack			stack;
		BArray<BExpHolder>		sequence;
		BArray<BExpHolder>			includes;
		BArray<BExpHolder>			declares;
		BMap<BString, BClassHolder>		classMap;
		BMap<BString, FunctionHolder>	functionMap;

#ifdef linux
		pthread_t			pausedHandle;
		pthread_cond_t 		thread_cond;
		pthread_mutex_t		thread_mutex;
#endif

#ifdef _WIN32
		HANDLE				pausedHandle;
#endif
	};

}

#endif